package com.kitbox.hl.meshow.common.web;


import com.google.gson.Gson;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class RequestCheck {
    /*******
     * 是否重复提交
     * @param httpServletRequest
     * @return
     */
    public static boolean sameRequestCheck(HttpServletRequest httpServletRequest){
        Gson gson=new Gson();
        String params= gson.toJson(httpServletRequest.getParameterMap());
        String url=httpServletRequest.getRequestURI();
        Map<String,String> map=new HashMap<String,String>();
        map.put(url, params);
        String nowUrlParams=map.toString();//
        Object preUrlParams=httpServletRequest.getSession().getAttribute("repeatData");

        if(preUrlParams!=null)//如果上一个数据为null,表示还没有访问页面
        {
            if(preUrlParams.toString().equals(nowUrlParams))//如果上次url+数据和本次url+数据相同，则表示城府添加数据
            {
                return true;
            }
        }
        httpServletRequest.getSession().setAttribute("repeatData", nowUrlParams);
        httpServletRequest.getSession().setAttribute(url, System.currentTimeMillis());
        return false;
    }


    /*****
     * 距离上次请求的时间
     * @param httpServletRequest
     * @return
     */
    public static Long repeatRequestTime(HttpServletRequest httpServletRequest){
        String url=httpServletRequest.getRequestURI();
        Long preUrlTime= 0l;
        try {
            preUrlTime=(Long) httpServletRequest.getSession().getAttribute(url);
        } catch (Exception e) {}
        if (preUrlTime==null)
            preUrlTime= 0l;
        long time=System.currentTimeMillis()-preUrlTime;
        return time;


    }
}
